import torch
from grid_env_ideal_obs_repeat_task import *
from grid_agent import *
from checkpoint_utils import *
from maze_factory import *
from replay_config import *
import argparse
import json
import sys
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.lines import Line2D
from sklearn.manifold import TSNE
import random
from sklearn.decomposition import PCA
from matplotlib.animation import FuncAnimation
from sklearn.cluster import KMeans
import threading
import mplcursors
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from scipy.spatial import KDTree
from sklearn.linear_model import LinearRegression
import umap
from ripser import ripser
from persim import plot_diagrams
from scipy.spatial.distance import pdist, squareform
from scipy.spatial.distance import cdist
from sklearn import svm
from sklearn import metrics
from sklearn.linear_model import LogisticRegression
import torch.nn as nn
import torch.optim as optim
from sklearn.neighbors import KernelDensity
from scipy.stats import entropy



def progress_bar(current, total, barLength = 100):
    percent = float(current) * 100 / total
    arrow = '-' * int(percent/100 * barLength - 1) + '>'
    spaces = ' ' * (barLength - len(arrow))

    print('Progress: [%s%s] %d %%' % (arrow, spaces, percent), end='\r')
    sys.stdout.flush()

@partial(jax.jit, static_argnums=(3,))
def model_forward(variables, state, x, model):
    """ forward pass of the model
    """
    return model.apply(variables, state, x)

@jit
def get_action(y):
    return jnp.argmax(y)
get_action_vmap = jax.vmap(get_action)

# load landscape and states from file
def load_task(pth = "./logs/task.json", display = True):
    # open json file
    with open(pth, "r") as f:
        data = json.load(f)
        landscape = data["data"]
        state = data["state"]
        goal = data["goal"]
        if display:
            print("state: ", state)
            print("goal: ", goal)
            print("landscape: ", landscape)
    return landscape, state, goal

def main():

    """ parse arguments
    """
    rpl_config = ReplayConfig()

    parser = argparse.ArgumentParser()
    parser.add_argument("--model_pth", type=str, default=rpl_config.model_pth)
    parser.add_argument("--map_size", type=int, default=rpl_config.map_size)
    parser.add_argument("--task_pth", type=str, default=rpl_config.task_pth)
    parser.add_argument("--log_pth", type=str, default=rpl_config.log_pth)
    parser.add_argument("--nn_size", type=int, default=rpl_config.nn_size)
    parser.add_argument("--nn_type", type=str, default=rpl_config.nn_type)
    parser.add_argument("--show_kf", type=str, default=rpl_config.show_kf)
    parser.add_argument("--visualization", type=str, default=rpl_config.visualization)
    parser.add_argument("--video_output", type=str, default=rpl_config.video_output)
    parser.add_argument("--life_duration", type=int, default=rpl_config.life_duration)
    parser.add_argument("--cl_type", type=str, default='net1')

    args = parser.parse_args()

    rpl_config.model_pth = args.model_pth
    rpl_config.map_size = args.map_size
    rpl_config.task_pth = args.task_pth
    rpl_config.log_pth = args.log_pth
    rpl_config.nn_size = args.nn_size
    rpl_config.nn_type = args.nn_type
    rpl_config.show_kf = args.show_kf
    rpl_config.visualization = args.visualization
    rpl_config.video_output = args.video_output
    rpl_config.life_duration = args.life_duration
    cl_type = args.cl_type

    nn_type = ''
    if rpl_config.nn_type == "vanilla":
        nn_type = "vanilla"
    elif rpl_config.nn_type == "gru":
        nn_type = "gru"

    def load_data(nn_type, seq_len, redundancy, diverse_set_capacity):

        rnn_limit_rings_file_name = "./logs/rnn_limit_ring_collection_" + nn_type + "_" + str(seq_len) + "_" + str(redundancy) + "_" + str(diverse_set_capacity) + ".npz"

        # 载入 npz 文件
        rnn_limit_rings_file = np.load(rnn_limit_rings_file_name)

        # 获取 npz 文件中的所有对象名称
        matrix_names = rnn_limit_rings_file.files

        rnn_limit_rings = []

        # 遍历对象名称，访问和操作每个矩阵对象
        for name in matrix_names:
            matrix = rnn_limit_rings_file[name]
            rnn_limit_rings.append(matrix)
            print("shape of matrix: ", np.shape(matrix))

        rnn_limit_rings = np.array(rnn_limit_rings)
        return rnn_limit_rings
    
    configs = [
            [nn_type, 6, 1, 100],
            # [nn_type, 9, 1, 100],
            [nn_type, 15, 1, 100],
        ]
    
    rnn_limit_rings_collection = []
    for i in range(len(configs)):
        raw_data_matrix = load_data(configs[i][0], configs[i][1], configs[i][2], configs[i][3])
        raw_data_linear = raw_data_matrix.reshape(raw_data_matrix.shape[0]*raw_data_matrix.shape[1]*raw_data_matrix.shape[2]*raw_data_matrix.shape[3],raw_data_matrix.shape[4])
        rnn_limit_rings_collection.append(raw_data_linear)

    # 将 rnn_limit_rings_collection 的所有元素拼接起来
    rnn_limit_rings_collection_all = np.concatenate(rnn_limit_rings_collection, axis=0)
    

    indices = torch.randperm(rnn_limit_rings_collection[0].shape[0])
    rnn_limit_rings_collection[0] = rnn_limit_rings_collection[0][indices]
    indices1 = torch.randperm(rnn_limit_rings_collection[1].shape[0])
    rnn_limit_rings_collection[1] = rnn_limit_rings_collection[1][indices1]
    A = rnn_limit_rings_collection[0]
    B = rnn_limit_rings_collection[1]

    # indices = torch.randperm(rnn_limit_rings_collection[0].shape[0])
    # rnn_limit_rings_collection[0] = rnn_limit_rings_collection[0][indices]
    # indices = torch.randperm(rnn_limit_rings_collection[0].shape[0])
    # rnn_limit_rings_collection[0] = rnn_limit_rings_collection[0][indices]
    # indices = torch.randperm(rnn_limit_rings_collection[0].shape[0])
    # rnn_limit_rings_collection[0] = rnn_limit_rings_collection[0][indices]
    # indices = torch.randperm(rnn_limit_rings_collection[0].shape[0])
    # rnn_limit_rings_collection[0] = rnn_limit_rings_collection[0][indices]
    # A = rnn_limit_rings_collection[0][:rnn_limit_rings_collection[0].shape[0]//2]
    # B = rnn_limit_rings_collection[0][rnn_limit_rings_collection[0].shape[0]//2:]

    n_samples = 2000
    rnd_idx_0 = np.random.choice(A.shape[0], n_samples, replace=False)
    rnd_idx_1 = np.random.choice(B.shape[0], n_samples, replace=False)
    A = A[rnd_idx_0]
    B = B[rnd_idx_1]

    # 生成两个随机数组，形状和 A/B 一样
    A_rnd = np.random.uniform(low=A.min(), high=A.max(), size=A.shape)
    B_rnd = np.random.uniform(low=B.min(), high=B.max(), size=B.shape)
    
    # 生成和 B_rnd 一样的正态分布数据
    # B_rnd = np.random.normal(loc=B.mean(), scale=B.std(), size=B.shape)

    print("Fit KDE models")

    # bandwidth_ = "silverman"
    bandwidth_ = 0.4

    # Fit KDE models
    kde_a = KernelDensity(kernel='gaussian', bandwidth=bandwidth_).fit(A)
    kde_b = KernelDensity(kernel='gaussian', bandwidth=bandwidth_).fit(B)

    print("Evaluate log-likelihood of each dataset on the other's KDE")
    # Evaluate log-likelihood of each dataset on the other's KDE  
    log_likelihood_a = kde_a.score_samples(B) 
    log_likelihood_b = kde_b.score_samples(A)

    # print the bandwidth of kde_a
    print("bandwidth of kde_a: ", kde_a.bandwidth_)

    print("Calculate KL divergence")
    # Calculate KL divergence       
    kl_div = entropy(log_likelihood_a, log_likelihood_b) 

    print('KL divergence between distributions A and B:')
    print(kl_div)
    
    # # Fit KDE models
    # kde_a = KernelDensity(kernel='gaussian', bandwidth=bandwidth_).fit(A_rnd)
    # # kde_b = KernelDensity(kernel='gaussian', bandwidth=bandwidth_).fit(B_rnd)
    # log_likelihood_a = kde_a.score_samples(B)
    # # log_likelihood_a = kde_a.score_samples(B_rnd)
    # log_likelihood_b = kde_b.score_samples(A_rnd)
    # kl_div = entropy(log_likelihood_a, log_likelihood_b) 
    # print('KL divergence between distributions A_rnd and B:')
    # print(kl_div)

    # sample_rnd = np.random.uniform(low=-1, high=1, size=A.shape)
    # sample_rnd_B = np.random.uniform(low=-1, high=1, size=A.shape)
    # indices = torch.randperm(sample_rnd.shape[0])
    # sample_rnd = sample_rnd[indices]
    # A = sample_rnd[:sample_rnd.shape[0]//2]
    # B = sample_rnd[sample_rnd.shape[0]//2:]

    # # A = sample_rnd
    # # B = sample_rnd_B

    # # Fit KDE models
    # kde_a = KernelDensity(kernel='gaussian', bandwidth=bandwidth_).fit(A)
    # kde_b = KernelDensity(kernel='gaussian', bandwidth=bandwidth_).fit(B)

    # print("Evaluate log-likelihood of each dataset on the other's KDE")
    # # Evaluate log-likelihood of each dataset on the other's KDE  
    # log_likelihood_a = kde_a.score_samples(B) 
    # log_likelihood_b = kde_b.score_samples(A)

    # # print the bandwidth of kde_a
    # print("bandwidth of kde_a: ", kde_a.bandwidth_)

    # print("Calculate KL divergence")
    # # Calculate KL divergence       
    # kl_div = entropy(log_likelihood_a, log_likelihood_b) 

    # print('KL divergence between distributions A and B:')
    # print(kl_div)



if __name__ == "__main__":
    main()
